package org.semanticflash.rdf.triple
{
	
	
	
	import com.bigflexlib.ds.container.zet.IRSet;
	





	public class RDFTripleUtil
	{


		
		// this is not the most performant way to do this, je sais
		public static function testGraphEquivalence( triples1:Array, triples2:Array ):Boolean
		{
			
			// basic test
			if ( triples1.length != triples2.length )
				return false;
			
			// all grounded triples must be equal
			// sort on grounded
			
			var g1:Array = triples1.filter( filterFunction_keepGrounded );
			var g2:Array = triples2.filter( filterFunction_keepGrounded );				
			
			
			if ( g1.length != g2.length )
				return false;
				
			g1 = g1.sortOn( "nt" );			
			g2 = g2.sortOn( "nt" );
			
			if ( ! arrsAreEqual( g1, g2 ) )
				return false;
			
			// time to test non-grounded triples
			
			g1 = triples1.filter( filterFunction_keepNonGrounded );
			g2 = triples2.filter( filterFunction_keepNonGrounded );				
			
			g1 = g1.sortOn( "ntNoBNodes" );			
			g2 = g2.sortOn( "ntNoBNodes" );								
			
			if ( ! arrsAreEqual( g1, g2 ) )
				return false;
			
			return true;		
		
		}



		// same elements in the same order
		private static function arrsAreEqual( arr1:Array, arr2:Array ):Boolean
		{
			if ( arr1.length != arr2.length )
				return false;
			
			var l:int = arr1.length;
			for ( var i:int=0; i<l ; i++ )
				if ( arr1[i] != arr2[i] )
					return false;				

			return true;
		}




		public static function filterFunction_keepGrounded( t:RDFTriple, index:int, array:Array ):Boolean
		{
			return t.isGrounded	;	
		}
		
		public static function filterFunction_keepNonGrounded( t:RDFTriple, index:int, array:Array ):Boolean
		{
			return ! t.isGrounded	;	
		}



	}

}